﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Analysis.Entities;
using System.Web.Services;
using Analysis.DAL.Repositories;

namespace Analysis.Admin
{
    public partial class Dashboard : Analysis.Code.BasePage
    {
        private static Guid UserId;
        //private static readonly string defoultOrderString = "Report;Address;Assort;MML;Sales;UM;Settings";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
	        {
		        UserId = CurrentUserId; //сохраняем ид пользователя
	        }

            if (IsAuthentificated)
            {
                bool isAdmin = IsAdmin;
                User user = CurrentUser;

                //ищем строку порядка кнопок
                String orderString = APRepository.GetUserParam(UserId, "OrderButtons");
                //если есть запись, то меняем порядок, иначе выводим по умолчанию
                if (!String.IsNullOrEmpty(orderString))
                {
                    string[] orderArr = orderString.Split(';');
                    if (orderArr.Length > 0)
                    { 
                        string id;
                        Control[] tempControls = new Control[sortable_container.Controls.Count]; 
                        sortable_container.Controls.CopyTo(tempControls,0);
                        List<Control> controls = tempControls.ToList();
                        sortable_container.Controls.Clear();
                        for (int i = 0; i < orderArr.Length; i++)
			            {
                            id = orderArr[i];
                            foreach(Control item in controls)
	                        {
                                if (item.ID == id)
                                {
                                    sortable_container.Controls.Add(item);
                                    controls.Remove(item);
                                    break;
                                }
	                        }
			            }
                        //добавляем оставшиеся контролы
                        if (controls.Count > 0)
                        {
                            foreach(Control item in controls)
	                        {
                                sortable_container.Controls.Add(item);
	                        }
                        }
                    }
                }
                //сокрытие кнопок страниц
                Report.Visible = isAdmin || user.IsPageReport;
                Address.Visible = isAdmin || user.IsPageAddress;
                Assort.Visible = isAdmin || user.IsPageAssort;
                MML.Visible = isAdmin || user.IsPageMML;
                Sales.Visible = isAdmin || user.IsPageSale;
                UM.Visible = isAdmin || user.IsPageUser;
                Settings.Visible = isAdmin || user.IsPageSetup;
            }
        }

        [WebMethod]
        public static void SaveOrder(string order)
        {
            if (!String.IsNullOrEmpty(order) && UserId != Guid.Empty)
            {
                APRepository.SetUserParam(UserId, "OrderButtons", order);
            }
            
        }
    }
}